home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZRELBIT < prev    next >
Text File  |  1991-10-25  |  6KB  |  268 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zrelbit.c */
  21. /* Relational, boolean, and bit operators for GhostScript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "dict.h"
  26. #include "store.h"
  27.  
  28. /* Forward references */
  29. int obj_compare(P2(os_ptr, int));
  30.  
  31. /* eq */
  32. int
  33. zeq(register os_ptr op)
  34. {    register os_ptr op1 = op - 1;
  35. #define eq_check_read(opp)\
  36.   switch ( r_type(opp) )\
  37.    {    case t_string: case t_array: case t_mixedarray: case t_shortarray:\
  38.       check_read(*opp); break;\
  39.     case t_dictionary: check_dict_read(*opp);\
  40.    }
  41.     eq_check_read(op);
  42.     eq_check_read(op1);
  43.     make_bool(op1, (obj_eq(op1, op) ? 1 : 0));
  44.     pop(1);
  45.     return 0;
  46. }
  47.  
  48. /* ne */
  49. int
  50. zne(register os_ptr op)
  51. {    /* We'll just be lazy and use eq. */
  52.     int code = zeq(op);
  53.     if ( !code ) op[-1].value.index ^= 1;
  54.     return code;
  55. }
  56.  
  57. /* ge */
  58. int
  59. zge(register os_ptr op)
  60. {    int code = obj_compare(op, 4+2);
  61.     if ( code < 0 ) return code;
  62.     make_bool(op - 1, code);
  63.     pop(1);
  64.     return 0;
  65. }
  66.  
  67. /* gt */
  68. int
  69. zgt(register os_ptr op)
  70. {    int code = obj_compare(op, 4);
  71.     if ( code < 0 ) return code;
  72.     make_bool(op - 1, code);
  73.     pop(1);
  74.     return 0;
  75. }
  76.  
  77. /* le */
  78. int
  79. zle(register os_ptr op)
  80. {    int code = obj_compare(op, 2+1);
  81.     if ( code < 0 ) return code;
  82.     make_bool(op - 1, code);
  83.     pop(1);
  84.     return 0;
  85. }
  86.  
  87. /* lt */
  88. int
  89. zlt(register os_ptr op)
  90. {    int code = obj_compare(op, 1);
  91.     if ( code < 0 ) return code;
  92.     make_bool(op - 1, code);
  93.     pop(1);
  94.     return 0;
  95. }
  96.  
  97. /* max */
  98. int
  99. zmax(register os_ptr op)
  100. {    int code = obj_compare(op, 1);
  101.     if ( code < 0 ) return code;
  102.     if ( code )
  103.        {    ref_assign(op - 1, op);
  104.        }
  105.     pop(1);
  106.     return 0;
  107. }
  108.  
  109. /* min */
  110. int
  111. zmin(register os_ptr op)
  112. {    int code = obj_compare(op, 4);
  113.     if ( code < 0 ) return code;
  114.     if ( code )
  115.        {    ref_assign(op - 1, op);
  116.        }
  117.     pop(1);
  118.     return 0;
  119. }
  120.  
  121. /* and */
  122. int
  123. zand(register os_ptr op)
  124. {    check_type(op[-1], r_type(op));
  125.     switch ( r_type(op) )
  126.        {
  127.     case t_boolean: op[-1].value.index &= op->value.index; break;
  128.     case t_integer: op[-1].value.intval &= op->value.intval; break;
  129.     default: return e_typecheck;
  130.        }
  131.     pop(1);
  132.     return 0;
  133. }
  134.  
  135. /* not */
  136. int
  137. znot(register os_ptr op)
  138. {    switch ( r_type(op) )
  139.        {
  140.     case t_boolean: op->value.index = !op->value.index; break;
  141.     case t_integer: op->value.intval = ~op->value.intval; break;
  142.     default: return e_typecheck;
  143.        }
  144.     return 0;
  145. }
  146.  
  147. /* or */
  148. int
  149. zor(register os_ptr op)
  150. {    check_type(op[-1], r_type(op));
  151.     switch ( r_type(op) )
  152.        {
  153.     case t_boolean: op[-1].value.index |= op->value.index; break;
  154.     case t_integer: op[-1].value.intval |= op->value.intval; break;
  155.     default: return e_typecheck;
  156.        }
  157.     pop(1);
  158.     return 0;
  159. }
  160.  
  161. /* xor */
  162. int
  163. zxor(register os_ptr op)
  164. {    check_type(op[-1], r_type(op));
  165.     switch ( r_type(op) )
  166.        {
  167.     case t_boolean: op[-1].value.index ^= op->value.index; break;
  168.     case t_integer: op[-1].value.intval ^= op->value.intval; break;
  169.     default: return e_typecheck;
  170.        }
  171.     pop(1);
  172.     return 0;
  173. }
  174.  
  175. /* bitshift */
  176. int
  177. zbitshift(register os_ptr op)
  178. {    int shift;
  179.     check_type(op[-1], t_integer);
  180.     check_type(*op, t_integer);
  181.     if ( op->value.intval < -31 || op->value.intval > 31 )
  182.         op[-1].value.intval = 0;
  183.     else if ( (shift = op->value.intval) < 0 )
  184.         op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
  185.     else
  186.         op[-1].value.intval <<= shift;
  187.     pop(1);
  188.     return 0;
  189. }
  190.  
  191. /* ------ Initialization procedure ------ */
  192.  
  193. op_def zrelbit_op_defs[] = {
  194.     {"2and", zand},
  195.     {"2bitshift", zbitshift},
  196.     {"2eq", zeq},
  197.     {"2ge", zge},
  198.     {"2gt", zgt},
  199.     {"2le", zle},
  200.     {"2lt", zlt},
  201.     {"2max", zmax},
  202.     {"2min", zmin},
  203.     {"2ne", zne},
  204.     {"1not", znot},
  205.     {"2or", zor},
  206.     {"2xor", zxor},
  207.     op_def_end(0)
  208. };
  209.  
  210. /* ------ Internal routines ------ */
  211.  
  212. /* Compare two operands (both numeric, or both strings). */
  213. /* Compute a result: 4 if >, 2 if =, 1 if <. */
  214. /* If the result matches a 1-bit in the mask, return 1, */
  215. /* otherwise return 0. */
  216. /* If the comparison fails, return a (negative) error code. */
  217. int
  218. obj_compare(register os_ptr op, int mask)
  219. {
  220. #define op1 (op-1)
  221.     float real1, real2;
  222.     switch ( r_type(op1) )
  223.        {
  224.     case t_integer:
  225.         switch ( r_type(op) )
  226.            {
  227.         case t_integer:
  228.             if ( op1->value.intval > op->value.intval )
  229.                 mask >>= 2;
  230.             else if ( op1->value.intval == op->value.intval )
  231.                 mask >>= 1;
  232.             return (mask & 1);
  233.         case t_real:
  234.             real1 = op1->value.intval;
  235.             real2 = op->value.realval;
  236.             break;
  237.         default: return e_typecheck;
  238.            }
  239.         break;
  240.     case t_real:
  241.         real1 = op1->value.realval;
  242.         switch ( r_type(op) )
  243.            {
  244.         case t_integer:
  245.             real2 = op->value.intval;
  246.             break;
  247.         case t_real:
  248.             real2 = op->value.realval;
  249.             break;
  250.         default: return e_typecheck;
  251.            }
  252.         break;
  253.     case t_string:
  254.         check_read(*op1);
  255.         check_read_type(*op, t_string);
  256.         mask >>= (bytes_compare(op1->value.bytes, r_size(op1),
  257.                 op->value.bytes, r_size(op)) + 1);
  258.         return (mask & 1);
  259.     default: return e_typecheck;
  260.        }
  261.     if ( real1 > real2 )
  262.         mask >>= 2;
  263.     else if ( real1 == real2 )
  264.         mask >>= 1;
  265.     return (mask & 1);
  266. #undef op1
  267. }
  268.